//
//  PartAlumViewController.m
//  u76ho
//
//  Created by m-air-01 on 16/2/3.
//  Copyright © 2016年 LV. All rights reserved.
//

#import "PartAlumViewController.h"
#import "AlumPicCollectionViewCell.h"
#import "PictureModel.h"
@interface PartAlumViewController ()
<UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,TongBuAlumDelegate>
{
    UICollectionView * _collectionView;
    NSMutableArray * dataSource;
    NSMutableArray * selectedArr;
    
    CGSize size;
}

@end

@implementation PartAlumViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self main];
   // [self addBackItem];
    self.navigationItem.title = @"选择同步相册";
    //[self addNavgationBar];
    [self addItems];
    // Do any additional setup after loading the view.
}
-(void)main
{
//    mypics = 5637;
    
    selectedArr = [[NSMutableArray alloc]init];
    dataSource = [[NSMutableArray alloc]init];
    CGFloat picWidth = (SCREEN_WIDTH - SIZETO6PlusW(15)*2-SIZETO6W(6)*2)/3;
    size = CGSizeMake(picWidth, picWidth+20);
    UICollectionViewFlowLayout * flow = [[UICollectionViewFlowLayout alloc]init];
    flow.minimumInteritemSpacing = SIZETO6W(6);
    flow.minimumLineSpacing = SIZETO6W(6);
    flow.scrollDirection = UICollectionViewScrollDirectionVertical;
    flow.sectionInset = UIEdgeInsetsMake(SIZETO6W(6), SIZETO6W(6), SIZETO6W(6), SIZETO6W(6));
    
    _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT-64) collectionViewLayout:flow];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.backgroundColor = [UIColor whiteColor];
    [_collectionView registerClass:[AlumPicCollectionViewCell class] forCellWithReuseIdentifier:@"AlumPicCollectionViewCell"];
    [self.view addSubview:_collectionView];
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self.view setBackgroundColor:UIColorFromRGB(0xf1f3f2)];
    [self requestData];

}
//去拉取相册
-(void)requestData
{

    [U76HONet minePictureWithaction:@"" code:[Singleton share].code andDataBlock:^(NSArray *arr) {
        if (dataSource.count!=0) {
            [dataSource removeAllObjects];
        }
        [dataSource addObjectsFromArray:arr];
        if (_mypic == nil) {
            for (int i = 0; i<arr.count; i++) {
                [selectedArr addObject:@"0"];
            }
        }
        else
        {
            // 分解mypic
            NSRange range = [_mypic rangeOfString:@"|"];
            if (range.location != NSNotFound) {
                NSArray * temparr = [_mypic componentsSeparatedByString:@"|"];
                for (int i = 0; i<arr.count; i++) {
                    PictureModel * model = arr[i];
                    [selectedArr addObject:@"0"];
                    for (int j =0; j<temparr.count; j++) {
                        if ([temparr[j] isEqualToString:model.ids]) {
                            [selectedArr replaceObjectAtIndex:i withObject:@"1"];
                        }
                    }
                }
            }
            else
            {
                for (int i = 0; i<arr.count; i++) {
                    PictureModel * model = arr[i];
                    [selectedArr addObject:@"0"];
                    
                    if ([_mypic isEqualToString:model.ids]) {
                        [selectedArr replaceObjectAtIndex:i withObject:@"1"];
                    }
                }

            }
        }
        
        [_collectionView reloadData];
        NSLog(@"获取我的图片库数据成功了");
    }];
}
#pragma mark - _collectionViewDelegate
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return dataSource.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    AlumPicCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"AlumPicCollectionViewCell" forIndexPath:indexPath];
    if (dataSource.count!=0) {
        cell.model = dataSource[indexPath.row];
        cell.flag = selectedArr[indexPath.row];
    }
    return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    PictureModel * model = [dataSource objectAtIndex:indexPath.row];
    NSLog(@"id 是%@",model.ids);
    //
    
        NSString * str = selectedArr[indexPath.item];
        if ([str isEqualToString:@"0"]) {
            [selectedArr replaceObjectAtIndex:indexPath.item withObject:@"1"];
        }
        else
        {
            [selectedArr replaceObjectAtIndex:indexPath.item withObject:@"0"];
        }
    
    [_collectionView reloadData];
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return size;
}

#pragma  mark - 返回按钮图标
-(void)addBackItem{
    UIButton * backItem = [UIButton buttonWithType:UIButtonTypeCustom];
    backItem.frame = CGRectMake(0, 0, 9, 17);
    [backItem setImage:[UIImage imageNamed:@"返回图标"] forState:UIControlStateNormal];
    [backItem addTarget:self action:@selector(backItem:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:backItem];
}
-(void)backItem:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - addRightItem
-(void)addItems
{
    UIButton * finishBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    finishBtn.frame = CGRectMake(0, 0, 40, 40);
    [finishBtn setTitle:@"完成" forState:UIControlStateNormal];
    [finishBtn setTitleColor:UIColorFromRGB(0x333944) forState:UIControlStateNormal];
    [finishBtn addTarget:self action:@selector(finishAction:) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem * rightItem = [[UIBarButtonItem alloc]initWithCustomView:finishBtn];
    self.navigationItem.rightBarButtonItem = rightItem;
}
-(void)finishAction:(UIButton *)btn
{
    NSLog(@"完成了");
    
    NSMutableArray * resultArr = [[NSMutableArray alloc]init];
    for (int i = 0; i<selectedArr.count; i++) {
        NSString * str = selectedArr[i];
        if ([str isEqualToString:@"1"]) {
            PictureModel * model = [dataSource objectAtIndex:i];
            [resultArr addObject:model.ids];
        }
    }
    if (resultArr.count!=0) {
        NSString * lastStr = [resultArr componentsJoinedByString:@"|"];
        NSLog(@"lastStr is %@",lastStr);
        [self.delegate tongbuAlummypic:lastStr];
        [self.navigationController popViewControllerAnimated:YES];
    }
    else
    {
        NSLog(@"是空的");
        [self.delegate tongbuAlummypic:@""];
        [self.navigationController popViewControllerAnimated:YES];
    }
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
